package com.hxy.modules.demo.service.impl;

import com.hxy.modules.common.common.Constant;
import com.hxy.modules.common.exception.MyException;
import com.hxy.modules.common.page.Page;
import com.hxy.modules.common.page.PageHelper;
import com.hxy.modules.common.utils.StringUtils;
import com.hxy.modules.common.utils.UserUtils;
import com.hxy.modules.common.utils.Utils;
import com.hxy.modules.demo.dao.MerchantsAuditDao;
import com.hxy.modules.demo.entity.MerchantsAuditEntity;
import com.hxy.modules.demo.service.MerchantsAuditService;
import com.hxy.modules.sys.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class MerchantsAuditServiceImpl implements MerchantsAuditService {

    @Autowired
    private MerchantsAuditDao  merchantsAuditDao;

    @Override
    public Page<MerchantsAuditEntity> findPage(MerchantsAuditEntity merchantsAuditEntity, int pageNum) {
        PageHelper.startPage(pageNum, Constant.pageSize);
        merchantsAuditDao.queryList(merchantsAuditEntity);
        return PageHelper.endPage();
    }

    @Override
    public MerchantsAuditEntity queryObject(String id) {
        if(StringUtils.isEmpty(id)){
            new MyException("id不为空!");
        }
        return merchantsAuditDao.queryObject(id);
    }

    @Override
    public void save(MerchantsAuditEntity merchantsAuditEntity) {
        merchantsAuditEntity.setCreatTime(new Date());
        merchantsAuditEntity.setId(Utils.uuid());

        UserEntity currentUser = UserUtils.getCurrentUser();
        merchantsAuditEntity.setCode(Utils.getCode("D"));
        merchantsAuditEntity.setStatus(Constant.ActStauts.DRAFT.getValue());
        merchantsAuditEntity.setCreateId(currentUser.getId());
        merchantsAuditEntity.setCreateTime(new Date());

        merchantsAuditDao.save(merchantsAuditEntity);
    }

    @Override
    public void update(MerchantsAuditEntity merchantsAuditEntity) {
        if(StringUtils.isEmpty(merchantsAuditEntity.getId())){
            throw new MyException("id不能为空");
        }
        merchantsAuditEntity.setUpdateId( UserUtils.getCurrentUserId());
        merchantsAuditEntity.setUpdateTime(new Date());
        merchantsAuditDao.update(merchantsAuditEntity);
    }

    @Override
    public int delete(String id) {
        return merchantsAuditDao.delete(id);
    }
}
